package com.mt.api.modules.controller;

import com.mt.common.response.WebPageResponse;
import com.mt.common.response.WebResponse;
import com.mt.common.response.WebResponseUtils;
import com.mt.log.annotation.Log;
import com.mt.api.modules.domain.bo.*;
import com.mt.api.modules.domain.vo.InstoreStatByInstoreNoVo;
import com.mt.api.modules.service.AppInstoreService;
import com.mt.api.util.ParamsValid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * @author plover
 * @date 2020-07-06
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "入库管理")
@RequestMapping("/app/instore")
public class AppInstoreController {

    private final AppInstoreService instoreService;


    @PostMapping("/add")
    @ApiOperation(value = "", notes = "入库单提交接口", httpMethod = "POST")
    @Log("入库新增")
    public WebResponse addInstore(@RequestBody AppInstoreAddBo resources) {
        ParamsValid valid = new ParamsValid();
        valid.validList("请选择商品入库", resources.getInstoreDetails());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return instoreService.addInstore(resources);
    }

    @PostMapping("/edit")
    @ApiOperation(value = "", notes = "入库单修改接口", httpMethod = "POST")
    @Log("入库修改")
    public WebResponse editInstore(@RequestBody AppInstoreAddBo resources) {
        ParamsValid valid = new ParamsValid();
        valid.validList("入库明细不能为空", resources.getInstoreDetails());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return instoreService.editInstore(resources);
    }

    @PostMapping("/cancel")
    @ApiOperation(value = "", notes = "入库单撤销接口", httpMethod = "POST")
    @Log("入库单撤销接口")
    public WebResponse cancelInstore(@RequestBody InstoreCancelBo bo) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("入库单id不能为空", bo.getInstoreId());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return instoreService.cancelInstore(bo);
    }


    @GetMapping("/statByInstoreNo")
    @ApiOperation(value = "", notes = "入库记录(按单据分组)", httpMethod = "GET")
    public WebPageResponse<InstoreStatByInstoreNoVo> statByInstoreNo(InstoreStatPageBo bo) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("pageNum不能为空", bo.getPageNum())
                .validNotNull("pageSize不能为空", bo.getPageSize());
        if (!valid.isValid()) {
            return WebResponseUtils.errorPageParam(valid.getMsg());
        }
        return instoreService.statByInstoreNo(bo);
    }

    @GetMapping("/detail")
    @Log("查询入库明细")
    public WebResponse instoreDetail(String instoreNo) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("入库单号", instoreNo);
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return instoreService.instoreDetail(instoreNo);
    }

    /**
     * 统计入库数据，细分到sku
     */
    @GetMapping("/statBySku")
    public WebResponse statBySku(AppInstoreStatBo instoreStatBo) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("beginDate", instoreStatBo.getBeginDate());
        valid.validNotNull("endDate", instoreStatBo.getEndDate());
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return instoreService.statBySku(instoreStatBo);
    }

    @GetMapping("/getInstoreDetail")
    @ApiOperation(value = "", notes = "入库明细查询", httpMethod = "GET")
    public WebResponse getInstoreDetail(Integer instoreId) {
        ParamsValid valid = new ParamsValid();
        valid.validNotNull("入库id不能为空", instoreId);
        if (!valid.isValid()) {
            return WebResponseUtils.errorParam(valid.getMsg());
        }
        return instoreService.getInstoreDetail(instoreId);
    }

}